﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace Forums
{
    public partial class CreateATopicPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtSubject.Focus();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtBody.Text.Length != 0 && txtSubject.Text.Length != 0)
            {
                lblCreateValidator.Visible = false;
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CentForumsDB"].ConnectionString);
                cmd.CommandText = string.Format("INSERT INTO dbo.topic (refers_to, thread_subject, thread_body, date_posted, user_accnt, deleted) VALUES({0},'{1}','{2}', getdate(),{3}, 0)", Request.QueryString["topicId"], txtSubject.Text, txtBody.Text, Session["user"]);
                try
                {
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    txtBody.Text = string.Empty;
                    txtSubject.Text = string.Empty;
                    Response.Redirect("ForumTopicPage.aspx?topicId=" + Request.QueryString["topicId"]);
                    // Response.Redirect("CreateATopicPage.aspx?topicId="+ Request.QueryString["topicId"]);

                }
                catch (Exception ex)
                {
                    lblCreateValidator.Visible = true;
                    lblCreateValidator.Text = ex.Message;
                }
                finally
                {
                    cmd.Connection.Close();
                }
            }
            else
            {
                lblCreateValidator.Visible = true;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            lblCreateValidator.Visible = false;
            txtBody.Text = string.Empty;
            txtSubject.Text = string.Empty;
        }
    }
}
